package com.xframe.xdal.core.util.redis;


import com.xframe.xdal.config.RedisConfig;
import com.xframe.xdal.core.util.redis.command.*;
import com.xframe.xdal.core.util.redis.pool.IRedisPool;
import com.xframe.xdal.core.util.redis.pool.RedisPoolCreate;
import lombok.extern.log4j.Log4j2;
import redis.clients.jedis.commands.BasicCommands;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Log4j2
public class RedisAccess {



    private static String defKey = "";
    public static void init(RedisConfig redisConfig){
        List<RedisConfig> list = new ArrayList<>();
        list.add(redisConfig);
        init(list);
    }
    public static void init(List<RedisConfig> redisConfigList){

        try {

            for(int i = 0; i < redisConfigList.size(); i++){
                RedisConfig redisConfig = redisConfigList.get(i);
                initRedisConfig(redisConfig);
                if(i == 0){
                    defKey = redisConfig.getName();
                }
            }


        }catch (Exception e){
            log.error(e);
        }
    }

    private static void initRedisConfig(RedisConfig redisConfig){
        try {
            IRedisPool pool = RedisPoolCreate.init(redisConfig);
            if(pool != null) {
                RedisCommand redisCommand = new RedisCommand(pool);
                commands.put(redisConfig.getName(),redisCommand);
                log.info("redis {} Ok",redisConfig.getHost());
            }
        }catch (Exception e){
            log.error(e);
        }
    }



    private static Map<String,RedisCommand> commands = new HashMap<>();




    public static <T> T getClient(){
        return (T) commands.get(defKey).getClient();
    }
    public static <T> T getClient(String name){
        return (T) commands.get(name).getClient();
    }

    public static <T> void close(T client){
        commands.get(defKey).close(client);
    }
    public static <T> void close(String name,T client){
        commands.get(name).close(client);
    }


    public static ListCommand getListCmd(){
        return getCmd(defKey,RedisCommandEnum.LIST);
    }
    public static ListCommand getListCmd(String name){
        return getCmd(name,RedisCommandEnum.LIST);
    }
    public static KeyCommand getKeyCmd(){
        return getCmd(defKey,RedisCommandEnum.KEY);
    }
    public static KeyCommand getKeyCmd(String name){
        return getCmd(name,RedisCommandEnum.KEY);
    }
    public static SetCommand getSetCmd(){
        return getCmd(defKey,RedisCommandEnum.SET);
    }
    public static SetCommand getSetCmd(String name){
        return getCmd(name,RedisCommandEnum.SET);
    }
    public static HashCommand getHashCmd(){
        return getCmd(defKey,RedisCommandEnum.HASH);
    }
    public static HashCommand getHashCmd(String name){
        return getCmd(name,RedisCommandEnum.HASH);
    }

    public static GetCommand getGetCmd(){
        return getCmd(defKey,RedisCommandEnum.GET);
    }
    public static GetCommand getGetCmd(String name){
        return getCmd(name,RedisCommandEnum.GET);
    }

    public static GatherCommand getGatherCommand(){
        return getCmd(defKey,RedisCommandEnum.GATHER);
    }

    public static GatherCommand getGatherCommand(String name){
        return getCmd(name,RedisCommandEnum.GATHER);
    }


    private static <T> T getCmd(String name,RedisCommandEnum redisCommandEnum){
        return (T)commands.get(name).getCmd(redisCommandEnum);
    }


    public static String select(int index) {
        return commands.get(defKey).select(index);
    }

    public static String ping() {
        return commands.get(defKey).ping();
    }





}
